

from fastapi import FastAPI

from app.api.api import api_router
from fastapi.middleware.cors import CORSMiddleware
import logging

from app.config.config import settings
from app.config.logger_config import setup_logging
from app.db.clickhouse_db import get_clickhouse_pool, ClickHouseHTTPPool
from app.runner.scheduler_task import start_scheduler

app = FastAPI(title="My Project", version="0.1.0")

app.include_router(api_router, prefix="/api/v1")
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)




@app.on_event("startup")
async def startup():
    setup_logging()
    clickHouseHttPPool: ClickHouseHTTPPool = get_clickhouse_pool()
    clickHouseHttPPool._init_pool()
    start_scheduler()
    logging.info(f"project [{settings.app_name}] have started...")


@app.on_event("shutdown")
async def shutdown():
    print(f"project [{settings.app_name}]  have closed...")
